﻿using Developer.Core.FrameWork.GlobalVar;
using Developer.Core.FrameWork.HelpFile;
using Developer.Core.FrameWork.RegisterCenter.Consul;
using Developer.Core.FrameWork.RegisterCenter.Options;
using Developer.Core.FrameWork.RegisterCenter.Service;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Developer.Core.FrameWork.RegisterCenter.Extensions
{
    /// <summary>
    /// 服务注册扩展
    /// </summary>
    public static class ServiceRegisterCollectionExtensions
    {
        public static IServiceCollection AddServiceRegister(this IServiceCollection services)
        {
            AddServiceRegister(services, option => { });
            return services;
        }

        public static IServiceCollection AddServiceRegister(this IServiceCollection services,Action<ServiceRegisterOptions> action)
        {
            // 0、是否开启Consul服务注册
            if (!GlobalVars.ConsulRegister)
                return services;

            // 1、服务注册配置选项注入IOC
            services.Configure<ServiceRegisterOptions>(action);

            // 2、consul注册到容器
            services.AddSingleton<IServiceRegister, ConsulServiceRegister>();

            // 3、开始服务启动自动注册
            services.AddHostedService<AutoRegisterServiceByHost>();  // 核心，寄宿在aspnetcore的主机中
            return services;
        }
    }
}
